package com.powermanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.powermanager.R;
import com.powermanager.bean.DayBean;
import com.powermanager.bean.LineBean;
import com.powermanager.bean.TimeBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.OptionPicker;
import cn.qqtheme.framework.picker.TimePicker;

/**
 * Created by lqy on 16/7/5.
 */
public class AddTimerActivity extends BaseActivity {
    @Bind(R.id.mLayoutLine)
    RelativeLayout mLayoutLine;
    @Bind(R.id.mLayoutTimertype)
    RelativeLayout mLayoutTimertype;
    @Bind(R.id.mLayoutON)
    RelativeLayout mLayoutON;
    @Bind(R.id.mLayoutOFF)
    RelativeLayout mLayoutOFF;
    @Bind(R.id.mTvLine)
    TextView mTvLine;
    @Bind(R.id.mTvType)
    TextView mTvType;
    @Bind(R.id.mTvON)
    TextView mTvON;
    @Bind(R.id.mTvOFF)
    TextView mTvOFF;
    @Bind(R.id.mBtnOK)
    Button mBtnOK;
    @Bind(R.id.mBtnCancle)
    Button mBtnCancle;
    private OptionPicker optionPicker;
    private TimePicker onTimePicker;
    private TimePicker offTimePicker;
    private LineBean lineBean = new LineBean();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addtimer);
        ButterKnife.bind(this);
        lineBean.setTag(1);
        lineBean.setName("总线");
        optionPicker = new OptionPicker(this, new String[]{
                "总线", "分路1", "分路2", "分路3", "分路4", "分路5", "分路6", "分路7", "分路8", "分路9"
        });
        optionPicker.setSelectedIndex(0);
        optionPicker.setTextSize(20);
        optionPicker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int position, String option) {
                lineBean.setTag(position+1);
                lineBean.setName(option);
                mTvLine.setText(option);
            }

        });
        onTimePicker = new TimePicker(this, TimePicker.HOUR_MINUTE_SECOND);
        onTimePicker.setTopLineVisible(false);
        onTimePicker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
            @Override
            public void onTimePicked(String hour, String minute, String second) {
                Toast.makeText(AddTimerActivity.this, hour + ":" + minute + ":" + second, Toast.LENGTH_SHORT).show();
                mTvON.setText(hour + "/" + minute + "/" + second);
            }
        });
        offTimePicker = new TimePicker(this, TimePicker.HOUR_MINUTE_SECOND);
        offTimePicker.setTopLineVisible(false);
        offTimePicker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
            @Override
            public void onTimePicked(String hour, String minute, String second) {
                Toast.makeText(AddTimerActivity.this, hour + ":" + minute + ":" + second, Toast.LENGTH_SHORT).show();
                mTvOFF.setText(hour + "/" + minute + "/" + second);
            }
        });
    }

    @OnClick({R.id.mLayoutLine, R.id.mLayoutTimertype, R.id.mLayoutON, R.id.mLayoutOFF,R.id.mBtnOK, R.id.mBtnCancle})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mLayoutLine:
                optionPicker.show();
                break;
            case R.id.mLayoutTimertype:
                startActivityForResult(new Intent(AddTimerActivity.this, TimeTypeActivity.class), 1002);
                break;
            case R.id.mLayoutON:
                onTimePicker.show();
                break;
            case R.id.mLayoutOFF:
                offTimePicker.show();
                break;
            case R.id.mBtnOK:
                if (TextUtils.equals(mTvON.getText().toString().trim(),"--/--/--")||TextUtils.equals(mTvON.getText().toString().trim(),"--/--/--")){
                    Toast.makeText(AddTimerActivity.this,"请输入开始时间或结束时间",Toast.LENGTH_SHORT).show();
                    return;
                }
                TimeBean timeBean = new TimeBean();
                timeBean.setLine(lineBean);
                timeBean.setType(mTvType.getText().toString().trim());
                timeBean.setOnTime(mTvON.getText().toString().trim());
                timeBean.setOffTime(mTvOFF.getText().toString().trim());
                Intent intent  = new Intent();
                intent.putExtra("data",timeBean);
                setResult(RESULT_OK,intent);
                finish();
                break;
            case R.id.mBtnCancle:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String result = "";
        if (resultCode == RESULT_OK && requestCode == 1002) {
            ArrayList<DayBean> results = data.getParcelableArrayListExtra("data");
            for (int i = 0; i < results.size(); i++) {
                result = result + results.get(i).name + ",";
                System.out.println(results.get(i).name);
            }
            mTvType.setText(result.substring(0, result.length() - 1));
        }

    }


}
